package com.mohansever.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mohansever.service.UserService;
import com.mohansever.util.ErrStatusEnum;
import com.mohansever.util.LoginToken;
import com.mohansever.util.PageData;
import com.mohansever.util.ResponseUtil;

@Controller
@RequestMapping(value = "/v1/sever")
public class ManagerController extends BaseController{

	@Resource
	private UserService userService;

	@RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object login(HttpServletRequest request,@RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","userPwd"}, pd)) {
				repUtil = userService.mangerLogin(pd);
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 分页获取用户列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/userList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getUserList(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","role","queryPage","eachPageSize"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = userService.getPageList(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}

	/**
	 * 删除用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteUser", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object delete(HttpServletRequest request,@RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","targetUserId"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = userService.deleteUser(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	
	/**
	 *改变用户状态
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/updateStatus", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object updateStatus(HttpServletRequest request,@RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			
			if (validataParms(new String[]{"userId","token","targetUserId","userStatus"}, pd)) {
				
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = userService.editUserStatus(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
}
